package org.csu.mypetstore.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    // 根据用户id查询该用户购物车表
    @GetMapping("/carts/{userId}")
    public CommonResponse<List<Cart>> getCartList(@PathVariable("userId") String userId){
        return cartService.getCartListByUserId(userId);
    }
    // 根据用户id与itemId查询购物车项
    @GetMapping("/carts/{userId}/items/{id}")
    public CommonResponse<Cart> getCartById(@PathVariable("userId") String userId, @PathVariable("id") String itemId){
        return cartService.getCartById(userId, itemId);
    }
    // 增加购物车项
    @PostMapping("carts/{userId}/items/{id}")
    public CommonResponse<Integer> addCartById(@PathVariable("userId") String userId,
                                               @PathVariable("id") String itemId){
        return cartService.addCart(userId, itemId);
    }
    // 删除购物车项
    @DeleteMapping("/carts/{userId}/items/{id}")
    public CommonResponse<Integer> deleteCartById(@PathVariable("userId") String userId,
                                                  @PathVariable("id") String itemId){
        return cartService.deleteCart(userId, itemId);
    }
    // 修改购物车项
    /*
    根据购物车需求分析，用户对于购物车项进行修改的操作如下：
    +1操作：购物车表中quantity一列在原有基础上+1
    -1操作：购物车表中quantity一列在原有基础上-1，如果低于或等于0，则这一项直接被删除
    修改操作：购物车中quantity按照用户自定义的数据进行修改，用户输入的数据必须大于0
     */
    @PutMapping("/carts/{userId}/items/{id}")
    public CommonResponse<Integer> changeCartQuantity(@PathVariable("userId") String userId,
                                                      @PathVariable("id") String itemId,
                                                      @RequestParam("quantity") int quantity){
        return cartService.changeCart(userId, itemId, quantity);
    }

}
